using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class SetConnection : Form
    {
        private EventScavengerDAL sqlEventSource;
        public SetConnection()
        {
            InitializeComponent();
        }

        public DialogResult ShowSetConnection()
        {
            cboServer.Text = Properties.Settings.Default.SQLServer;
            cboDatabase.Text = Properties.Settings.Default.Database;
            txtUsername.Text = Properties.Settings.Default.UserName;
            txtPassword.Text = Properties.Settings.Default.Password;
            numericUpDownSQLTimeOut.Value = Properties.Settings.Default.SQLCMDTimeOut;
            return this.ShowDialog();
        }

        #region Form events
        private void SetConnection_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.RecentSQLServers != null)
            {
                cboServer.Items.Clear();
                foreach (string server in Properties.Settings.Default.RecentSQLServers)
                {
                    cboServer.Items.Add(server);
                }
            }
            if (Properties.Settings.Default.RecentDatabases != null)
            {
                cboDatabase.Items.Clear();
                foreach (string database in Properties.Settings.Default.RecentDatabases)
                {
                    cboDatabase.Items.Add(database);
                }
            }
            SetUserNameBackColor();
        }
        #endregion

        #region Button events
        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (TestConnection(false))
            {
                Properties.Settings.Default.SQLServer = cboServer.Text;
                Properties.Settings.Default.Database = cboDatabase.Text;
                Properties.Settings.Default.UserName = txtUsername.Text;
                Properties.Settings.Default.Password = txtPassword.Text;
                if (Properties.Settings.Default.RecentSQLServers == null)
                    Properties.Settings.Default.RecentSQLServers = new System.Collections.Specialized.StringCollection();
                if (!Properties.Settings.Default.RecentSQLServers.Contains(cboServer.Text))
                {
                    Properties.Settings.Default.RecentSQLServers.Add(cboServer.Text);
                }
                if (Properties.Settings.Default.RecentDatabases == null)
                    Properties.Settings.Default.RecentDatabases = new System.Collections.Specialized.StringCollection();
                if (!Properties.Settings.Default.RecentDatabases.Contains(cboDatabase.Text))
                {
                    Properties.Settings.Default.RecentDatabases.Add(cboDatabase.Text);
                }
                Properties.Settings.Default.SQLCMDTimeOut = Convert.ToInt32(numericUpDownSQLTimeOut.Value);

                Properties.Settings.Default.Save();
                DialogResult = DialogResult.OK;
                Close();
            }
        }
        private void cmdTest_Click(object sender, EventArgs e)
        {
            TestConnection(true);
        } 
        #endregion

        #region Textbox events
        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            SetUserNameBackColor();
        }
        private void txtUsername_Leave(object sender, EventArgs e)
        {
            SetUserNameBackColor();
        }
        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            SetUserNameBackColor();
        }
        private void txtPassword_Leave(object sender, EventArgs e)
        {
            SetUserNameBackColor();
        }
        #endregion

        #region Private methods
        private bool TestConnection(bool prompt)
        {
            bool success = false;
            int recordCount;
            sqlEventSource = new EventScavengerDAL();
            sqlEventSource.Server = cboServer.Text;
            sqlEventSource.Database = cboDatabase.Text;
            sqlEventSource.UserName = txtUsername.Text;
            sqlEventSource.Password = txtPassword.Text;
            sqlEventSource.CommandTimeout = Properties.Settings.Default.SQLCMDTimeOut;
            sqlEventSource.SetConnection();
            try
            {
                recordCount = sqlEventSource.TestConnection();
                if (prompt)
                    MessageBox.Show("Success! What else did you expect? ;)\r\nThere are " + recordCount.ToString() + " record(s) in the table", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return success;
        }
        private void SetUserNameBackColor()
        {
            if (txtUsername.Text.Length > 0)
            {
                txtUsername.BackColor = SystemColors.Window;
                txtPassword.BackColor = SystemColors.Window;
            }
            else
            {
                txtUsername.BackColor = SystemColors.ControlLight;
                txtPassword.BackColor = SystemColors.ControlLight;
            }
        }
        #endregion



    }
}